﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Gencode.Common.Generics
{
	public class ServiceProvider : IServiceProvider
	{
		private Dictionary<Type, object> services = new Dictionary<Type, object>();

		public ServiceProvider()
		{
		}

		/// <summary>
		/// Returns the specified service or null if the service doesn't exist.
		/// </summary>
		/// <param name="serviceType"></param>
		/// <returns></returns>
		public object GetService(Type serviceType)
		{
			object service = null;
			services.TryGetValue(serviceType, out service);
			return service;
		}

		/// <summary>
		/// Adds a service to the service provider's list of services using the object's type as the key.
		/// </summary>
		/// <param name="service"></param>
		public void AddService(object service)
		{
			if(services.ContainsKey(service.GetType()))
				services.Remove(service.GetType());

			services.Add(service.GetType(), service);
		}

		/// <summary>
		/// Adds a service to the service provider's list of services using the provided Type as the key.
		/// </summary>
		/// <param name="service"></param>
		public void AddService(object service, Type key)
		{
			if (services.ContainsKey(key))
				services.Remove(key);

			services.Add(key, service);
		}
	}
}
